package com.company.threads;

/**
 * 线程打印奇数 偶数
 *
 * @author xiaohutu
 * @return {@link {@link null}}
 * @throws RuntimeException e
 * @data 2021/1/15 14:26
 */
public class ThreadA {

    private static volatile int i = 1;

    public static void main(String[] args) {
        final Object obj = new Object();
        Runnable runnable = () -> {
            synchronized (obj) {
                for (; i <= 10; ) {
//                    if (i % 3 == 1) {
//                        System.out.println(Thread.currentThread().getName() + " " + (i * 3 + 2));
//                    } else if (i % 3 == 2) {
//                        System.out.println(Thread.currentThread().getName() + " " + (i * 3 + 1));
//                    } else {
//                        System.out.println(Thread.currentThread().getName() + " " + (i * 3));
//                    }
                    System.out.println(Thread.currentThread().getName() + " " + (i));
                    i++;
                    try {
                        obj.notifyAll();
//                        obj.notify();
                        obj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
//                obj.notifyAll();
            }
        };
        Thread thread = new Thread(runnable, "111");
        Thread thread2 = new Thread(runnable, "222 ");
        Thread thread3 = new Thread(runnable, "333 ");
        thread.start();
        thread2.start();
        thread3.start();
//        try {
//            thread.join();
//            thread2.join();
//            thread3.join();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
    }

}
